#!/usr/bin/perl

$ENV{'QUERY_STRING'} =~ s/^([^_]+)_//;
$worker = $1;
$ENV{'QUERY_STRING'} =~ m/(\d+)$/;
$num = $1;

if($worker eq '' || !(-x "${worker}.cgi")){
       print("Content-Type: application/octet-stream\r\n\r\n");
       exit(0);
}
$mask = sprintf("%08x", 1 << ($num % &GetCPUNum()));
exec("/bin/taskset", $mask, "./${worker}.cgi");

print("Content-Type: application/octet-stream\r\n\r\n");
exit(0);

sub GetCPUNum{
        my($num) = 0;
        if(open(STAT, "</proc/stat")){
                while(<STAT>){
                        $num = $1 if(m/^cpu(\d+)/);
                }
                close(STAT);
        }
        return $num + 1;
}
